package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.StudentQueryDto;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.ClazzCountVo;
import com.itheima.vo.StudentItemVo;
import com.itheima.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean pageQuery(StudentQueryDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<StudentVo> page = studentMapper.query(dto);
        long total = page.getTotal();
        List<StudentVo> list = page.getResult();
        PageBean pageBean = new PageBean(total, list);
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        ids.forEach(id -> studentMapper.delete(id));
    }

    @Override
    public void insert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public Student findById(Integer id) {
        Student student = studentMapper.findById(id);
        return student;
    }

    @Override
    public void violationHandle(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }

    @Override
    public List<StudentItemVo> getStudentDegreeData() {
        return studentMapper.getStudentDegreeData();
    }

    @Override
    public ClazzCountVo getStudentCountData() {
        List<StudentItemVo> countList = studentMapper.getStudentCount();
        List<String> clazzList = countList.stream().map(StudentItemVo::getName).collect(Collectors.toList());
        List<Integer> dataList = countList.stream().map(StudentItemVo::getValue).collect(Collectors.toList());
        return new ClazzCountVo(clazzList, dataList);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }
}
